package com.zycfc.zsf.boot.serializer.jdk;

import com.zycfc.zsf.boot.serializer.*;
import java.io.*;

public class JdkSerializer implements ISerializer
{
    @Override
    public byte[] serialize(final Object obj) throws Exception {
        if (obj == null) {
            return new byte[0];
        }
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        final ObjectOutputStream output = new ObjectOutputStream(outputStream);
        output.writeObject(obj);
        output.flush();
        return outputStream.toByteArray();
    }
    
    @Override
    public Object deserialize(final byte[] bytes) throws Exception {
        if (null == bytes || bytes.length == 0) {
            return null;
        }
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        final ObjectInputStream input = new ObjectInputStream(inputStream);
        return input.readObject();
    }
}
